clc
clear all
%% 参数设置
size_ball = 15; % 小球的大小
size_temp_dot = 5;
semaphore_is_InLab = false; % 如果在实验室，则置一此信号量
if semaphore_is_InLab 
    config_io;
    LPTAdr = 20472; % 
    samp=1/1000; % 标签硬件延时
end
speed_ball_s = 0.5; % 小球运动速度
random_integer = randi([1, 100]);

color_red = [255,0,0];% 红色
color_white = [255, 255, 255];
color_black = [0, 0, 0];
color_yellow = [255, 255, 0];
color_ball = color_red;
decision_hashmap = zeros(1, 100);

if (mod(random_integer, 2) == 0)
    decision_hashmap(1, [3,7,13,18,23,27,32,37,  42,46,50,  54,58,  63,67,  72,76,  79,  83]) = 1; % 第i个元素置一表示第i步为小球错误运动方向  
    step_up_right = 8 + 2 * 2; % 由up转向right的第x步
    step_right_down = step_up_right + 8 + 2 * 3;
    step_down_left = step_right_down + 8 + 2 * 3;
    step_left_up = step_down_left + 6 + 2 * 3;
    step_up_right_2 = step_left_up + 6 + 2 * 2;
    step_right_down_2 = step_up_right_2 + 4 + 2 * 2;
    step_down_left_2 = step_right_down_2 + 4 + 2 * 2;
    step_left_up_2 = step_down_left_2 + 2 + 2 * 1; 
    step_up_end = step_left_up_2 + 2 + 2 * 1;
else
    decision_hashmap(1, [4,9,  14,17,24,  28,31,38,  42,47,  51,56,60,  65,68,  72,76,  80,  83]) = 1; % 第i个元素置一表示第i步为小球错误运动方向  
    step_up_right = 8 + 2 * 2; 
    step_right_down = step_up_right + 8 + 2 * 3;
    step_down_left = step_right_down + 8 + 2 * 3;
    step_left_up = step_down_left + 6 + 2 * 2;
    step_up_right_2 = step_left_up + 6 + 2 * 3;
    step_right_down_2 = step_up_right_2 + 4 + 2 * 2;
    step_down_left_2 = step_right_down_2 + 4 + 2 * 2;
    step_left_up_2 = step_down_left_2 + 2 + 2 * 1; 
    step_up_end = step_left_up_2 + 2 + 2 * 1;
end
% function step_xx_xx_cur = calculate_steps(step_xx_xx_prev, decision_hashmap)
%     i = 0;
%     
%     step_xx_xx_cur = step_xx_xx_prev + 8 + 2 * i;
%     if step_xx_xx_cur
% end
%% 引导
Screen('Preference', 'SkipSyncTests', 1);
Screen('Preference','SkipSyncTests',2);
HideCursor;
[w, wrect] = Screen('OpenWindow', 0, [0, 0, 0]);
centerX_screen = wrect(3) / 2; % 屏幕中心点x坐标
centerY_screen = wrect(4) / 2; % 屏幕中心点y坐标
instruction=Screen('MakeTexture',w,imread('xxxxxxxxxx\演示文稿1.TIF'));
Screen('DrawTexture',w,instruction,[]);%显示提示语
Screen('Flip',w)
%PTB硬件bug，不同电脑会有不同鬼畜按键
% [ keyIsDown, ~, keyCode ] = KbCheck;
% keyCode = find(keyCode, 1);
% if keyIsDown
%     ignoreKey=keyCode;
%     DisableKeysForKbCheck(ignoreKey);
% end
KbStrokeWait;%任意键继续

%% 绘制初始静态网格线
size_chessboard = 16; % 网盘的边长
length_chessboard = centerY_screen * 2 / (3 * 0.5 * size_chessboard); % 单个网格的边长
x_min_index_chessboard = centerX_screen - 8 * length_chessboard; % 网盘的左边界x坐标
x_max_index_chessboard = centerX_screen + 8 * length_chessboard; % 网盘的右边界x坐标
y_min_index_chessboard = centerY_screen - 8 * length_chessboard; % 网盘的上边界y坐标
y_max_index_chessboard = centerY_screen + 8 * length_chessboard; % 网盘的下边界y坐标
x_min_index_trace = centerX_screen - 4 * length_chessboard; % 轨迹的左边界x坐标
x_max_index_trace = centerX_screen + 4 * length_chessboard; % 轨迹的右边界x坐标
y_min_index_trace = centerY_screen - 4 * length_chessboard; % 轨迹的上边界y坐标
y_max_index_trace = centerY_screen + 4 * length_chessboard; % 轨迹的下边界y坐标
xy_4_trace_start = [x_min_index_trace-size_ball y_max_index_trace-size_ball x_min_index_trace+size_ball y_max_index_trace+size_ball];
xy_4_temp_start = [x_min_index_trace-size_temp_dot y_max_index_trace-size_temp_dot-length_chessboard x_min_index_trace+size_temp_dot y_max_index_trace+size_temp_dot-length_chessboard];
xy_4_target = [centerX_screen-size_ball centerY_screen-size_ball centerX_screen+size_ball centerY_screen+size_ball];

% 绘制网格
for i = 1 : (size_chessboard + 1)
    Screen('DrawLine', w, color_white, x_min_index_chessboard, y_min_index_chessboard + (i - 1) * length_chessboard, ...
        x_max_index_chessboard, y_min_index_chessboard + (i - 1) * length_chessboard, 0.5);
    Screen('DrawLine', w, color_white, x_min_index_chessboard + (i - 1) * length_chessboard, y_min_index_chessboard, ...
        x_min_index_chessboard + (i - 1) * length_chessboard, y_max_index_chessboard, 0.5);
   
end
Screen('DrawLine', w, color_yellow, centerX_screen - 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
    centerX_screen - 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen - 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
    centerX_screen + 4 * length_chessboard, centerY_screen - 4 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen + 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
    centerX_screen + 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen + 4 * length_chessboard, ...
    centerX_screen + 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
    centerX_screen - 2 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
    centerX_screen + 2 * length_chessboard, centerY_screen - 2 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen + 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
    centerX_screen + 2 * length_chessboard, centerY_screen + 2 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen, centerY_screen + 2 * length_chessboard, ...
    centerX_screen + 2 * length_chessboard, centerY_screen + 2 * length_chessboard, 3);
Screen('DrawLine', w, color_yellow, centerX_screen, centerY_screen, ...
    centerX_screen, centerY_screen + 2 * length_chessboard, 3);

% 绘制小球和target
Screen('FillOval', w, color_ball, xy_4_trace_start);
Screen('FillOval', w, color_white, xy_4_temp_start);
Screen('FillRect', w, color_yellow, xy_4_target); % target
Screen('Flip',w)

WaitSecs(3);

%% 动态小球 
for step = 1 : step_up_end
    if semaphore_is_InLab 
        lptwrite(LPTAdr, 0); % 初始化端口状态
        WaitSecs(samp);
    end   
    %%%%%%%%%%%%%%静态的网格和轨迹
    for i = 1 : (size_chessboard + 1)
    Screen('DrawLine', w, color_white, x_min_index_chessboard, y_min_index_chessboard + (i - 1) * length_chessboard, ...
        x_max_index_chessboard, y_min_index_chessboard + (i - 1) * length_chessboard, 0.5);
    Screen('DrawLine', w, color_white, x_min_index_chessboard + (i - 1) * length_chessboard, y_min_index_chessboard, ...
        x_min_index_chessboard + (i - 1) * length_chessboard, y_max_index_chessboard, 0.5);
    end

    Screen('DrawLine', w, color_yellow, centerX_screen - 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
        centerX_screen - 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen - 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
        centerX_screen + 4 * length_chessboard, centerY_screen - 4 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen + 4 * length_chessboard, centerY_screen - 4 * length_chessboard, ...
        centerX_screen + 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen + 4 * length_chessboard, ...
        centerX_screen + 4 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
        centerX_screen - 2 * length_chessboard, centerY_screen + 4 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen - 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
        centerX_screen + 2 * length_chessboard, centerY_screen - 2 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen + 2 * length_chessboard, centerY_screen - 2 * length_chessboard, ...
        centerX_screen + 2 * length_chessboard, centerY_screen + 2 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen, centerY_screen + 2 * length_chessboard, ...
        centerX_screen + 2 * length_chessboard, centerY_screen + 2 * length_chessboard, 3);
    Screen('DrawLine', w, color_yellow, centerX_screen, centerY_screen, ...
        centerX_screen, centerY_screen + 2 * length_chessboard, 3);
    Screen('FillRect', w, color_yellow, xy_4_target);
    %%%%%%%%%%%%%%%%%%%%%%%
    
    % 通过中间变量，记录上一步小球的位置
    if(step == 1)
        previous_y_index_up = xy_4_trace_start(2);
        previous_y_index_down = xy_4_trace_start(4);
        previous_x_index_left = xy_4_trace_start(1);
        previous_x_index_right = xy_4_trace_start(3);
        previous_y_temp_index_up   = xy_4_temp_start(2);
        previous_y_temp_index_down = xy_4_temp_start(4);
        previous_x_temp_index_left = xy_4_temp_start(1);
        previous_x_temp_index_right= xy_4_temp_start(3);
    else
        previous_y_index_up = current_y_index_up;
        previous_y_index_down = current_y_index_down;
        previous_x_index_left = current_x_index_left;
        previous_x_index_right = current_x_index_right;
        previous_y_temp_index_up    = current_y_temp_index_up;
        previous_y_temp_index_down  = current_y_temp_index_down;
        previous_x_temp_index_left  = current_x_temp_index_left;
        previous_x_temp_index_right = current_x_temp_index_right;
    end

    % 小球动态路径
    if ~decision_hashmap(step) % 正确
        if step <= step_up_right 
            current_y_index_up    = previous_y_index_up - length_chessboard;
            current_y_index_down  = previous_y_index_down - length_chessboard;
            current_x_index_left  = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up    = previous_y_temp_index_up   - length_chessboard;
            current_y_temp_index_down  = previous_y_temp_index_down - length_chessboard;
            current_x_temp_index_left  = previous_x_temp_index_left ;
            current_x_temp_index_right = previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_right_down 
            current_y_index_up    = previous_y_index_up   ;
            current_y_index_down  = previous_y_index_down ;
            current_x_index_left  = previous_x_index_left  + length_chessboard;
            current_x_index_right = previous_x_index_right + length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left + length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right+ length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_down_left         
            current_y_index_up    = previous_y_index_up     + length_chessboard;
            current_y_index_down  = previous_y_index_down + length_chessboard;
            current_x_index_left  = previous_x_index_left ;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   + length_chessboard; 
            current_y_temp_index_down = previous_y_temp_index_down + length_chessboard; 
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right ;           
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_left_up
            current_y_index_up    = previous_y_index_up   ;
            current_y_index_down  = previous_y_index_down ;
            current_x_index_left  = previous_x_index_left - length_chessboard;
            current_x_index_right = previous_x_index_right - length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left - length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right - length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_up_right_2
            current_y_index_up    = previous_y_index_up     - length_chessboard;
            current_y_index_down  = previous_y_index_down - length_chessboard;
            current_x_index_left  = previous_x_index_left ;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   - length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down - length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_right_down_2
            current_y_index_up    = previous_y_index_up   ;
            current_y_index_down  = previous_y_index_down ;
            current_x_index_left  = previous_x_index_left   + length_chessboard;
            current_x_index_right = previous_x_index_right + length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left + length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right+ length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_down_left_2
            current_y_index_up    = previous_y_index_up     + length_chessboard;
            current_y_index_down  = previous_y_index_down + length_chessboard;
            current_x_index_left  = previous_x_index_left ;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   + length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down + length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;            
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_left_up_2
            current_y_index_up    = previous_y_index_up   ;
            current_y_index_down  = previous_y_index_down ;
            current_x_index_left  = previous_x_index_left   - length_chessboard;
            current_x_index_right = previous_x_index_right - length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left - length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right- length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_up_end
            current_y_index_up    = previous_y_index_up     - length_chessboard;
            current_y_index_down  = previous_y_index_down - length_chessboard;
            current_x_index_left  = previous_x_index_left ;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   - length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down - length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        end
        
        % 打标签"1"（表示正确）
        if semaphore_is_InLab 
            lptwrite(LPTAdr, 1);
            WaitSecs(samp);
            lptwrite(LPTAdr, 0);
        end
    else % 错误
        if step <= step_up_right
            current_y_index_up = previous_y_index_up + length_chessboard;
            current_y_index_down = previous_y_index_down + length_chessboard;
            current_x_index_left = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up    = previous_y_temp_index_up   + length_chessboard;
            current_y_temp_index_down  = previous_y_temp_index_down + length_chessboard;
            current_x_temp_index_left  = previous_x_temp_index_left ; 
            current_x_temp_index_right = previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_right_down 
            current_y_index_up = previous_y_index_up;
            current_y_index_down = previous_y_index_down;
            current_x_index_left = previous_x_index_left - length_chessboard;
            current_x_index_right = previous_x_index_right - length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left - length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right- length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_down_left
            current_y_index_up = previous_y_index_up - length_chessboard;
            current_y_index_down = previous_y_index_down - length_chessboard;
            current_x_index_left = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   - length_chessboard; 
            current_y_temp_index_down = previous_y_temp_index_down - length_chessboard; 
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right ;           
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_left_up
            current_y_index_up = previous_y_index_up;
            current_y_index_down = previous_y_index_down;
            current_x_index_left = previous_x_index_left + length_chessboard;
            current_x_index_right = previous_x_index_right + length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left +length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right+ length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_up_right_2
            current_y_index_up = previous_y_index_up + length_chessboard;
            current_y_index_down = previous_y_index_down + length_chessboard;
            current_x_index_left = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   + length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down + length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_right_down_2
            current_y_index_up = previous_y_index_up;
            current_y_index_down = previous_y_index_down;
            current_x_index_left = previous_x_index_left - length_chessboard;
            current_x_index_right = previous_x_index_right - length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left - length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right- length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_down_left_2
            current_y_index_up = previous_y_index_up - length_chessboard;
            current_y_index_down = previous_y_index_down - length_chessboard;
            current_x_index_left = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   - length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down - length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;            
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_left_up_2
            current_y_index_up = previous_y_index_up;
            current_y_index_down = previous_y_index_down;
            current_x_index_left = previous_x_index_left + length_chessboard;
            current_x_index_right = previous_x_index_right + length_chessboard;
            current_y_temp_index_up   = previous_y_temp_index_up   ;
            current_y_temp_index_down = previous_y_temp_index_down ;
            current_x_temp_index_left = previous_x_temp_index_left + length_chessboard;
            current_x_temp_index_right= previous_x_temp_index_right+ length_chessboard;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        elseif step <= step_up_end
            current_y_index_up = previous_y_index_up + length_chessboard;
            current_y_index_down = previous_y_index_down + length_chessboard;
            current_x_index_left = previous_x_index_left;
            current_x_index_right = previous_x_index_right;
            current_y_temp_index_up   = previous_y_temp_index_up   + length_chessboard;
            current_y_temp_index_down = previous_y_temp_index_down + length_chessboard;
            current_x_temp_index_left = previous_x_temp_index_left ;
            current_x_temp_index_right= previous_x_temp_index_right;
            Screen('FillOval', w, color_ball, [current_x_index_left current_y_index_up ...
                current_x_index_right current_y_index_down]);
            Screen('FillOval', w, color_white, [current_x_temp_index_left current_y_temp_index_up ...
                current_x_temp_index_right current_y_temp_index_down]);
        end

        % 打标签"2"（表示错误）
        if semaphore_is_InLab 
            lptwrite(LPTAdr, 2);
            WaitSecs(samp);
            lptwrite(LPTAdr, 0);
        end
    end
    %%%%%%%%%%%%%%%%%%%%%%%

    Screen('Flip',w)
    WaitSecs(speed_ball_s);
end

%% 退出机制
while true
    [keyIsDown,~,keyCode]=KbCheck;
    if keyCode(27)
        break;
    end
end


Screen('CloseAll');

